﻿$(document).ready(function () {
    Show();

    $(".likeButton").click(function () {
        var id = $(this + "input")
        $(".hiddenID").val();
    });

});



$("#addNewComment").ready(function () {
    $("#addNewComment").click(function () {
        var comment = $("#CommentText").val();
        $.post("/Home/Create", { CommentText: comment }, function (data) {
            $("#myComments").empty();
            document.getElementById('CommentText').value = '';
        });

        Show();
    });

});

$(".likeButton").click(function () {
    var id = $(this + "input")
    $(".hiddenID").val();
});

function saveLike(where, id) {
    $.post("/Home/Save/" + id, "", function (data) {
        var tooltip = "";
        for (var i = 0; i < data.lenght; i++) {
            tooltip += data[i].User + ",";
        }
    
        $(where).html("You like this!");

        $("#likeList").attr("title", tooltip);
        $('#likeList').tipsy({ trigger: 'hover', fade: true, gravity: 'n' });

    });
    event.preventDefault();     // kemur í veg fyrir hopp
  }


function Show() {
    $.ajax({
            
		    type: "GET",
		    contentType: "application/json; charset=utf-8",
		    url: "/Home/Comments/",
		    data: "{}",
		    dataType: "json",
		    success: function (data) {
		        $("#commentTemplate").tmpl(data).appendTo("#myComments")
		        
		    },
		    error: function (xhr, err) {
		        // Note: just for debugging purposes!
		        alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
		        alert("responseText: " + xhr.responseText);
		    }
		});

}

